package servlet;

import dao.ProductTypeDao;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "QueryProductTypeServlet", value = "/QueryProductTypeServlet.s")
public class QueryProductTypeServlet extends HttpServlet {

    private ProductTypeDao dao = new ProductTypeDao();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        final String tname = request.getParameter("tname");
        final String status = request.getParameter("status");
        //获取当前页显示条数
        final String pageSize = request.getParameter("pageSize");
        //获取当前页码
        final String currentPage = request.getParameter("currentPage");
        //分页查询条件
        //把字符串转成int型数据
        int size = Integer.valueOf(pageSize);  //size表示每页多条数据
        int page = Integer.valueOf(currentPage);
        //分页查询条件 begin表示从表中第几条数据开始查 end 即size 表示每页多少条数据
        int begin = size * (page-1);
        int end = size;

        long total = dao.selectProductTypeCount(tname, status);
        //分页查询
        final List<Map<String, Object>> data = dao.selectProductType(tname, status, begin, size);
        //返回json对象
        Map<String,Object> result = new HashMap<>();
        result.put("data",data);
        result.put("total",total);
        final String json = new Gson().toJson(result);
        response.getWriter().append(json);
    }
}